package org.example.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler{

    @ExceptionHandler(Exception.class)
    public Object handleException(Exception e) {
        log.error("发生系统异常:", e);
        Map<String, Object> map = new HashMap<>();
        map.put("code", ErrorEnum.SERVER_ERROR.getCode());
        map.put("message", ErrorEnum.SERVER_ERROR.getMessage());
        return map;
    }

    @ExceptionHandler(ServiceException.class)
    public Object handleServiceException(ServiceException e) {
        log.error("发生业务异常:", e);
        Map<String, Object> map = new HashMap<>();
        map.put("code", e.getErrorEnum().getCode());
        map.put("message", e.getErrorEnum().getMessage());
        return map;
    }
}
